<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 *
 */
class Orders extends CI_Model  {
	/**
	 * Constructor
	 * @access protected
	 */
	function _construct()
	{
      // Call the Model constructor
      parent::_construct();
	}
	// Сохранение информации о клиенте и занесение их в группу
	function save_client($groupId,$agentId,$fio,$phone,$email,$country,$pasport,$age)
	{
		if($groupId==0)
		{
			$clientId=$this->createClient($fio,$phone,$email,$country,$pasport,$age);
			if($clientId)
			{
				$query="select id from groupclients order by id desc limit 1";
				$result=$this->db->query($query);
				if($result->num_rows()>0)
				{
					$groupId=$result->row()->id+1;
				}
				else
				{
					$groupId=1;
				}
				$query="insert into groupclients (id,clientId,agentId) values ($groupId,$clientId,$agentId)";
				$this->db->query($query);
				return $groupId;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$clientId=$this->createClient($fio,$phone,$email,$country,$pasport,$age);
			if($clientId)
			{
				$query="insert into groupclients (id,clientId,agentId) values ($groupId,$clientId,$agentId)";
				$this->db->query($query);
				return $groupId;
			}
			else
			{
				return false;
			}
		}
		
	}
	
	function get_tour_name($tourDayId){
		$query = "select Name from service where id=".$tourDayId;
		$result = $this->db->query($query);
		return $result->row()->Name;
	}
	
	function get_listOfTables_id ($tourDayId, $hotel){
		$query = "select id from listoftables where idService=".$tourDayId." and name='".$hotel."*'  ";
		$result = $this->db->query($query);
		return $result->row()->id;
	}
	
	function get_price ($idTable, $dateStart, $idRow){
		$query = "select price from price where idtable=".$idTable." and `date`='".$dateStart."' and idRow=".$idRow;
		$result = $this->db->query($query);
		return $result->row()->price;
	}
	
	function get_price_N ($clientId, $dateStart, $tourDayId, $idRow){
		$query = "select price,type from agentprice where userId=".$clientId." and `date`='".$dateStart."' and idservice=".$tourDayId." and idRow=".$idRow;
		$result = $this->db->query($query);
		if($result->num_rows()>0)
		{
			$data['priceN'] = $result->row()->price;
			$data['type'] = $result->row()->type;
		}
		else
		{
			$data['priceN'] = '0';
			$data['type'] = '1';
		}
		return $data;
	}
	
	function get_oto_email($otoId){
		$query = 'select email from users where id='.$otoId;
		$result = $this->db->query($query);
		return $result->row()->email;
	}
	
	function createClient($fio,$phone,$email,$country,$pasport,$age)
	{
		$query="insert into client (fio,phone,email,country,pasport,age)  values ('$fio','$phone','$email','$country','$pasport','$age')";
		$this->db->query($query);
		$query="select id from client order by id desc limit 1";
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			return $result->row()->id;
		}
		else
		{
			return false;
		}
	}
	function editClient($id,$fio,$phone,$email,$country,$pasport,$age)
	{
		$query="update set  fio='$fio',
							phone='$phone',
							email='$email',
							country='$country',
							pasport='$pasport',
							age='$age'
				where id=$id";
		$this->db->query($query);
	}
	function checkUser($pasport)
	{
		$query="Select * from client where pasport='$pasport'";
		$result=$this->db->query($query)->result_array();
		return json_encode($result);
	}
	//Создание комнат
	function createRoom($groupId,$type,$child,$dinner)
	{
		if($groupId==0)
		{
			
			$query="select id from grouprooms order by id desc limit 1";
			$result=$this->db->query($query);
			if($result->num_rows()>0)
			{
				$groupId=$result->row()->id+1;
			}
			else
			{
				$groupId=1;
			}
		}
		$query="insert into rooms (type,child,hb) values ($type,$child,$dinner)";
		$this->db->query($query);
		$query="Select id from rooms order by id desc limit 1";
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$id=$result->row()->id;
		}
		else
		{
			return false;
		}
		$query="insert into grouprooms (id,idroom) values ($groupId,$id)";
		$this->db->query($query);
		return $groupId;
	}
	function changeRoom($id,$type,$child,$dinner)
	{
		$query="update rooms set type=$type, child=$child, hb=$dinner where id=$id";
		$this->db->query($query);
	}
	
	//Создание, изменение опций 
	function createOptions($tourId)
	{
		$query="select mId from groupchanges order by id desc limit 1";
		$result=$this->db->query($query);
		if($result->num_rows()>0)
		{
			$groupId=$result->row()->id+1;
		}
		else
		{
			$groupId=1;
		}
		$query="select idOption,`desc` from listoptions where idservice=$tourId";
		$result=$this->db->query($query);
		foreach($result->result() as $row)
		{
			$query="insert into changes (idOption,value) values (".$row->idOption.",'".$row->desc."')";
			$this->db->query($query);
			$query="select id from changes order by id desc limit 1";
			$res=$this->db->query($query);
			if($res->num_rows()>0)
			{
				$id=$res->row()->id;
			}
			else
			{
				echo "WTF";
				$this->log('WTF 160');
				exit();
			}
			echo "id=".$id." groupId=".$groupId;
			$query="insert into groupchanges (mid,changesId) values ($groupId,$id)";
			$this->db->query($query);
		}
		return $groupId;
	}
	function editOption($id,$value)
	{
		$query="update changes set value='".$value."' where id=$id";
		$this->db->query($query);
	}
	
	function save_order_tmp($data){
		$this->db->select('id')->from('orders_tmp')->where('guid',$data['guid']);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			$str = $this->db->insert_string('orders_tmp', $data);
			$query = $this->db->query($str);
			
			$query = 'select id from orders_tmp where guid="'.$data['guid'].'"';
			$result = $this->db->query($query);
			return $result->row()->id;
		}
		else{
			return FALSE;
		}
	}
	function save_order ($data){

		#$this->log('save starts');
		$str = $this->db->insert_string('orders', $data);
		#$this->log('save starts...'.$str);
		$query = $this->db->query($str);
		#if ($query)
		#	$this->Log('all gooood');
		#else 
		#	$this->Log('all gone bad');
	}
	function get_orders_list($otoId, $type){
		//exit ($otoId);
		if ($type == 2){
			$query = 'select id, guid, userName, checkOne, checkTwo, notSpam from orders_tmp';
			$result = $this->db->query($query);
			$tmp = $result->result_array();			
		}
		else {
			$query = 'select id, guid, userName, checkOne, checkTwo, notSpam from orders_tmp where otoId='.$otoId;
			$result = $this->db->query($query);
			$tmp = $result->result_array();			
		}
		//exit (print_r($tmp));
		return $tmp;
	}
	function getlist($type,$id)
	{
		if($type==1){$where='prinId='.$id;}
		if($type==0){
			$where='otprId='.$id;
		}
		if($type==2){$where='1';}
		$query='select * from orders where '.$where.' order by id desc';
		$orders=$this->db->query($query);
		$responce=array();
		$i=0;
		foreach($orders->result() as $order)
		{
			$query='select name from service where id='.$order->serviceId;
			$result=$this->db->query($query);
			if($result->num_rows()>0)
			{
				$responce[$i]['serviceName']=$result->row()->name;
			}
			if($type==0||$type==2)
			{
				$query="Select name from users where id=".$order->prinId;
				$result=$this->db->query($query);
				if($result->num_rows()>0)
				{
					$responce[$i]['prinUser']=$result->row()->name;
				}
				else
				{
					$responce[$i]['prinUser']="Unknow";
				}
			}
			if($type==1||$type==2)
			{
				$query="Select name from users where id=".$order->otprId;
				$result=$this->db->query($query);
				if($result->num_rows()>0)
				{
					$responce[$i]['otprUser']=$result->row()->name;
				}
				else
				{
					$responce[$i]['otprUser']="Unknow";
				}
			}

			$responce[$i]['id']=$order->id;
			$responce[$i]['pax']=$order->pax;
			$responce[$i]['date']=$order->date;
			$responce[$i]['datestart']=$order->dateStart;
			$responce[$i]['confirmOtpr']=$order->ConfirmA;
			$responce[$i]['confirmPrin']=$order->ConfirmB;
			$responce[$i]['whishes']=$order->wishes;
			$responce[$i]['comments']=$order->comment;
			$responce[$i]['transfer']=$order->transfer;
			$responce[$i]['price']=$order->price;
			$responce[$i]['clientprice']=$order->clientPrice;
			$query="select idOption,value from changes where id in (select changesid from groupchanges where mId=".$order->changesId.")";
			$result=$this->db->query($query);
			$j=0;
			foreach($result->result() as $row)
			{
				$query="select name from options where id=".$row->idOption;
				$res=$this->db->query($query);
				if($res->num_rows()>0)
				{
					$responce[$i]['options'][$j]['value']=$row->value;
					$responce[$i]['options'][$j]['name']=$res->row()->name;
					$j++;
				}
			}
			$query="select * from client where id in (select clientId from groupclients where id=".$order->groupId." order by clientId)";
			$res=$this->db->query($query);
			$j=0;
			foreach($res->result() as $row)
			{
				$responce[$i]['clients'][$j]['phone']=$row->phone;
				$responce[$i]['clients'][$j]['fio']=$row->FIO;
				$responce[$i]['clients'][$j]['country']=$row->country;
				$responce[$i]['clients'][$j]['pasport']=$row->pasport;
				$responce[$i]['clients'][$j]['age']=$row->age;
				$responce[$i]['clients'][$j]['email']=$row->email;
				$j++;
			}
			$query="select * from rooms where id in (select idRoom from grouprooms where id=".$order->roomsId.")";
			$res=$this->db->query($query);
			$j=0;
			foreach($res->result() as $row)
			{
				$responce[$i]['rooms'][$j]['type']=$row->type;
				$responce[$i]['rooms'][$j]['chld']=$row->child;
				$responce[$i]['rooms'][$j]['hb']=$row->hb;
				$j++;
			}
			$i++;
		}
		return $responce;
	}
}